package com.qwiki.test;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;

import com.qwiki.xml.WikiPageAsyncTextifyHandler;
import com.qwiki.xml.WikiPageHandler;
import com.qwiki.xml.WikiPageMultiHandler;
import com.qwiki.xml.WikiXMLPageValidArticleFilter;
import com.qwiki.xml.WikiXmlEngine;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.xml.sax.SAXException;

public class textify {
  public static void main(String[] args) throws SAXException, IOException, InterruptedException {
    Logger.getRootLogger()
        .addAppender(new ConsoleAppender(new PatternLayout("%d{yyyy/MM/dd HH:mm:ss.SSS} %p [%c{1}] [%x] %m%n")));


    Options options = new Options();

    Option option = OptionBuilder.create("wikixml");
    option.setRequired(true);
    option.setArgs(1);
    option.setDescription("the path to the wikimedia xml file");
    options.addOption(option);

    option = OptionBuilder.create("numThreads");
    option.setRequired(true);
    option.setArgs(1);
    option.setDescription("the number of threads we want to run in parallel");
    options.addOption(option);

    option = OptionBuilder.create("output");
    option.setRequired(true);
    option.setArgs(1);
    option.setDescription("output file name");
    options.addOption(option);

    CommandLineParser cliParser = new BasicParser();

    String wikixml = null;
    int numThreads = 1;
    String outputFileName = null;
    PrintStream printer = null;

    try {
      CommandLine cli = cliParser.parse(options, args);
      wikixml = cli.getOptionValue("wikixml");
      numThreads = Integer.parseInt(cli.getOptionValue("numThreads"));
      outputFileName = cli.getOptionValue("output");
      printer = new PrintStream(Files.newOutputStream((new File(outputFileName)).toPath())) {
        @Override
        public synchronized void print(String string) {
          super.print(string);
        }
      };
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    WikiXmlEngine engine = new WikiXmlEngine(wikixml);

    WikiPageAsyncTextifyHandler[] handlers = new WikiPageAsyncTextifyHandler[numThreads];
    for (int i = 0; i < handlers.length; i++) {
      handlers[i] = new WikiPageAsyncTextifyHandler("test", i, printer);
      handlers[i].start();
    }
    WikiPageHandler handler = new WikiPageMultiHandler(handlers);
    handler.setFilter(new WikiXMLPageValidArticleFilter());
    engine.setHandler(handler);
    engine.process();
    for (int i = 0; i < handlers.length; i++) {
      handlers[i].drain();
      System.out.format("%1$d drained", i);
      handlers[i].join();
    }
  }
}
